﻿using BLL;
using CommonUtility;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;

namespace WebApplication
{
    public partial class Main : BasePage
    {
        protected string tg;
        protected string strMenu;

        protected void Page_Load(object sender, EventArgs e)
        {
            string action = Common.ToString(Request["Action"]);
            switch (action)
            {
                case "out":
                    Response.Write(LogOut());
                    Response.End();
                    break;

            }

            if (!IsPostBack && string.IsNullOrEmpty(action))
            {
                //获取菜单权限
                bllPermission perm = new bllPermission(oUser.UserID);
                strMenu = perm.Menu.Trim(',').ToUpper();

                tg = Server.UrlDecode(Common.ToString(Request["tg"]));
                if (tg.ToLower().IndexOf("funid") > -1)
                {
                    string[] param = tg.Split('&');
                    foreach (string item in param)
                    {
                        if (item.ToLower().IndexOf("funid") > -1)
                        {
                            string[] param1 = item.Split('=');
                            if (param1.Length > 1)
                            {
                                bllMessage.SetRead(param1[1], oUser);
                            }
                        }

                    }

                }
                lblUser.Text = oUser.Name;

                //显示记录条数
                string msgCount = bllMessage.GetMessageCount(oUser);
                if(string.IsNullOrEmpty(msgCount))
                {
                    lblMessage_Count.Visible = false;
                }
                else
                {
                    lblMessage_Count.Visible = true; lblMessage_Count.InnerHtml = msgCount;
                }

            }
        }

        private string LogOut()
        {
            try
            {
                bllLog.AddLog("", "Log out", Request.UserHostAddress, oUser.UserID);
            }
            catch (Exception ex)
            {
                Common.Application_Error_Server("Default.aspx.cs/LBExit_Click() " + oUser.UserID, ex);
            }

            Server.Transfer("Login.aspx", false);
            Response.Cookies.Clear();
            Session.Abandon();
            Session.Clear();
            return "";
        }
        private string GetMessageCount()
        {
            return bllMessage.GetMessageCount(oUser);
        }

        protected void LbExit_Click(object sender, EventArgs e)
        {
            LogOut();
        }
        protected void btnOut_Click(object sender, EventArgs e)
        {
            LogOut();
        }
    }
}